package com.ydj.controller;

import com.ydj.AppEnvironment;
import com.ydj.app.protocol.dto.ForecastQaItemDto;
import com.ydj.app.third.ai.AiModelService;
import com.ydj.app.third.ai.ChatFluxListener;
import com.ydj.app.third.ai.yuanqi.YuanQiManager;
import com.ydj.framework.annotation.AllowGuest;
import com.ydj.framework.exception.CommonException;
import com.ydj.framework.third.email.EmailManager;
import com.ydj.framework.third.tencentsms.TencentSmsManager;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 测试
 */
@Slf4j
@Controller
@RequestMapping("/test")
@ApiIgnore
public class TestController {

    @Autowired
    AppEnvironment appEnvironment;

    @Autowired
    private EmailManager emailManager;
    @Autowired
    private TencentSmsManager tencentSmsManager;

    @Autowired
    private AiModelService aiModelService;

    @Autowired
    private YuanQiManager yuanQiManager;

    @AllowGuest
    @ApiOperation(value = "Test-test")
    @RequestMapping("/test")
    @ResponseBody
    String test() throws CommonException {

//        tencentSmsManager.sendLoginVerifyCode("13798183943", "12345", 5);

        return "LuLuRWA V1.0"
                + (appEnvironment.isMaster() ? "主" : "从")
                + "<br>"
                + System.currentTimeMillis()
                + "<br>"
                + AppEnvironment.getBuildType();
    }

    @AllowGuest
    @ApiOperation(value = "Test-测试流式对话")
    @RequestMapping("/testChatFlex")
    @ResponseBody
    String testChatFlex() throws CommonException {
        List<ForecastQaItemDto> list = new ArrayList<>();
        list.add(createInput("您的企业名称", "", true, "我们和你们交个朋友"));
        list.add(createInput("您的业务介绍", "", true, "自媒体孵化机构"));
        list.add(createInput("您准备在国内做Pre-RWA还是香港RWA", "", true, "香港"));
        list.add(createInput("您做RWA的目的是战略布局还是募资", "", true, "战略布局"));
        list.add(createInput("您的企业/项目年收入", "", true, "8000KW"));
        list.add(createInput("您的企业/项目年增速", "", true, "20%左右"));
        list.add(createInput("请描述下您的企业上下游客户情况（100字以内）", "企业目标客户类型、客户数量、单个客户平均成交额", true, "目标客户是各大厂家，大概有100个左右，客单价一般有几十万"));
        list.add(createInput("您企业/项目过往融资情况", "融资时间规模，投资机构，股权或债权，债权请标记利率情况", true, "打算融资13亿"));
        list.add(createInput("您布局RWA前期预算是多少", "", true, "1亿"));
        list.add(createInput("您是否考虑借壳上市/上市公司合作", "", true, "与上市公司合作"));
        list.add(createCheckBox("找白露会客厅的目的（多选）", Arrays.asList(
                new ForecastQaItemDto.QaOptionsItem("报名白露私亨晚宴1688每位，每周日晚线下闭门深圳南山饭局。每次限12位").setChecked(true),
                new ForecastQaItemDto.QaOptionsItem("深度链接:年度先锋会员2980元/每年，内部深度资料，及参与白露组织的腾讯云会议线上大咖闭门交流").setChecked(true),
                new ForecastQaItemDto.QaOptionsItem("1V1咨询:单次付费咨询5000元/每小时，咨询您的项目RWA能否成功发行").setChecked(true),
                new ForecastQaItemDto.QaOptionsItem("RWA方案:企业RWA战略方案落地报告及路演，3W一份").setChecked(false),
                new ForecastQaItemDto.QaOptionsItem("顾问合作及拜师学习:月顾问/季度顾问/年顾问，3W起随缘，弟子班8.88W起，可定制需求，一线顶级大咖直接交流及线下由白露引荐见面，顶层战略咨询").setChecked(false),
                new ForecastQaItemDto.QaOptionsItem("免费自由交流+加入未经筛选的免费粉丝群").setChecked(false),
                new ForecastQaItemDto.QaOptionsItem("其他定制需求", "我希望能够...").setChecked(false)
        )));

//        aiModelService.chat(list, "0", new ChatFluxListener() {
//            @Override
//            public void flux(String content) {
//
//            }
//
//            @Override
//            public void error(Throwable e) {
//
//            }
//        });

        return  "OK";
    }

    @AllowGuest
    @ApiOperation(value = "Test-测试预测")
    @RequestMapping("/testForecast")
    @ResponseBody
    String testForecast() throws CommonException {
        List<ForecastQaItemDto> list = new ArrayList<>();
        list.add(createInput("您的企业名称", "", true, "我们和你们交个朋友"));
        list.add(createInput("您的业务介绍", "", true, "自媒体孵化机构"));
        list.add(createInput("您准备在国内做Pre-RWA还是香港RWA", "", true, "香港"));
        list.add(createInput("您做RWA的目的是战略布局还是募资", "", true, "战略布局"));
        list.add(createInput("您的企业/项目年收入", "", true, "8000KW"));
        list.add(createInput("您的企业/项目年增速", "", true, "20%左右"));
        list.add(createInput("请描述下您的企业上下游客户情况（100字以内）", "企业目标客户类型、客户数量、单个客户平均成交额", true, "目标客户是各大厂家，大概有100个左右，客单价一般有几十万"));
        list.add(createInput("您企业/项目过往融资情况", "融资时间规模，投资机构，股权或债权，债权请标记利率情况", true, "打算融资13亿"));
        list.add(createInput("您布局RWA前期预算是多少", "", true, "1亿"));
        list.add(createInput("您是否考虑借壳上市/上市公司合作", "", true, "与上市公司合作"));
        list.add(createCheckBox("找白露会客厅的目的（多选）", Arrays.asList(
                new ForecastQaItemDto.QaOptionsItem("报名白露私亨晚宴1688每位，每周日晚线下闭门深圳南山饭局。每次限12位").setChecked(true),
                new ForecastQaItemDto.QaOptionsItem("深度链接:年度先锋会员2980元/每年，内部深度资料，及参与白露组织的腾讯云会议线上大咖闭门交流").setChecked(true),
                new ForecastQaItemDto.QaOptionsItem("1V1咨询:单次付费咨询5000元/每小时，咨询您的项目RWA能否成功发行").setChecked(true),
                new ForecastQaItemDto.QaOptionsItem("RWA方案:企业RWA战略方案落地报告及路演，3W一份").setChecked(false),
                new ForecastQaItemDto.QaOptionsItem("顾问合作及拜师学习:月顾问/季度顾问/年顾问，3W起随缘，弟子班8.88W起，可定制需求，一线顶级大咖直接交流及线下由白露引荐见面，顶层战略咨询").setChecked(false),
                new ForecastQaItemDto.QaOptionsItem("免费自由交流+加入未经筛选的免费粉丝群").setChecked(false),
                new ForecastQaItemDto.QaOptionsItem("其他定制需求", "我希望能够...").setChecked(false)
        )));

        AiModelService.AiForecastResult deepseekForecastResult = aiModelService.getForecastResultByYuanQi(list, 0L);

        return  "问题: <br>"
                + ForecastQaItemDto.toQuestionPrompt(list).replace("\n", "<br>")
                + "<p>"
                + "答案: <br>"
                + ForecastQaItemDto.toAnswerPrompt(list).replace("\n", "<br>")
                + "<p>"
                + "评价:" + deepseekForecastResult.getResult()
                + "<p>"
                + "成功率:" + deepseekForecastResult.getRate() + "%";
    }

    private ForecastQaItemDto createInput(String label, String placeholder, Boolean required, String value) {
        ForecastQaItemDto item = new ForecastQaItemDto()
                .setLabel(label)
                .setType(ForecastQaItemDto.Type.InputBox)
                .setPlaceholder(placeholder)
                .setRequired(required)
                .setValue(value);
        return item;
    }

    private ForecastQaItemDto createCheckBox(String label, List<ForecastQaItemDto.QaOptionsItem> optionsItemList) {
        ForecastQaItemDto item = new ForecastQaItemDto()
                .setLabel(label)
                .setType(ForecastQaItemDto.Type.CheckBox)
                .setRequired(true)
                .setOptions(optionsItemList);
        return item;
    }
}